package com.wtwd.campus.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.common.RespEntity;
import com.wtwd.campus.entity.QueryOperationRecordVo;
import com.wtwd.campus.service.LogService;
import com.wtwd.campus.service.RedisService;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/operationLog")
public class OperationLogController {
    private final static Logger logger = LoggerFactory.getLogger(OperationLogController.class);

    @Autowired
    private RedisService redisService;
    @Autowired
    private LogService logService;

    /**
     * 获取操作模块
     *
     * @param orgCode
     * @param request
     * @return
     */
    @GetMapping("/getOperationModule/{orgCode}")
    public RespEntity getOperationModule(@PathVariable("orgCode") String orgCode,
                                         HttpServletRequest request) {
        logger.info("getOperationModule: orgCode={} ,schoolId={} ", orgCode);
        //获取用户的组织编号
        String userOrgCode = getUserOrgCode(request);
        if ("0".equals(orgCode)) {
            orgCode = userOrgCode;
        }
        List<String> modules = logService.getOperationModule(orgCode);
        return new RespEntity(RespCode.SUCCESS, modules);
    }

    /**
     * 条件查询获取操作记录列表
     *
     * @param data
     * @param request
     * @return
     */
    @PostMapping("/getOperationList")
    public RespEntity getOperationList(@RequestBody QueryOperationRecordVo data,
                                       HttpServletRequest request) {
        logger.info("getOperationList: data={}  ", data.toString());
        //获取用户的组织编号
        String userOrgCode = getUserOrgCode(request);
        if (StringUtils.isBlank(data.getOrgCode())) {
            data.setOrgCode(userOrgCode);
        }
        if(data.getMinTime()==0){
            data.setMinTime(null);
        }
        if(data.getMaxTime()==0){
            data.setMaxTime(null);
        }
        if(StringUtils.isBlank(data.getModule())){
            data.setModule(null);
        }
        if(StringUtils.isBlank(data.getUserName())){
            data.setUserName(null);
        }
        Map<String, Object> result = logService.getOperationRecordList(data);
        return new RespEntity(RespCode.SUCCESS, result);
    }


    /**
     * 获取用户组织编码
     *
     * @param request
     * @return
     */
    public String getUserOrgCode(HttpServletRequest request) {
        //获取用户的组织编号
        String token = request.getHeader(Constants.HEADER_TOKEN_KEY);
        Map<String, Object> msg = redisService.getUserInfoByToken(token);
        String userMsg = (String) msg.get("user");
        JSONObject jsonObject = JSONObject.parseObject(userMsg);
        String userOrgCode = (String) jsonObject.get("orgCode");
        return userOrgCode;
    }

    /**
     * 登录日志
     * @param jsonObject
     * @return
     */
    @PostMapping("/getLoginLog")
    public RespEntity getLoginLog(@RequestBody JSONObject jsonObject) {
        logger.info("getLoginLog: jsonObject={}  ", jsonObject);
        Integer currentPage = jsonObject.getInteger("currentPage");
        Integer pageSize = jsonObject.getInteger("pageSize");
        String orgCode = jsonObject.getString("orgCode");
        String name = jsonObject.getString("name");//用户明1
        Long startTime = jsonObject.getLong("startTime");
        Long endTime = jsonObject.getLong("endTime");
        Page<Object> page = new Page<>(currentPage, pageSize);
        Map<String, Object> restMap = logService.getLoginLogByCondition(orgCode, name, startTime, endTime,page);
        return new RespEntity(RespCode.SUCCESS,restMap);
    }
}
